package com.hungryinlindholmen;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 * 
 * LoginServlet creates a LoginDB object with username and password from the
 * request object, if they are correct the user will be forwarded to owner.jsp
 * else they go to loginfailed.jsp.
 * 
 * @author Olof Ekelund
 */

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
	super();

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
	LoginDB restaurant = new LoginDB(request.getParameter("username"),
		request.getParameter("password"));

	RequestDispatcher dispatcher;

	if (restaurant.loginUser()) {
	    request.getSession().setAttribute("restID", restaurant.getRestID());
	    request.getSession().setAttribute("loggedin", "true");
	    dispatcher = request.getRequestDispatcher("owner.jsp");
	} else {
	    dispatcher = request.getRequestDispatcher("loginfailed.jsp");
	}
	dispatcher.forward(request, response);
	return;
    }

}
